package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.GroupMemberChoiceBean;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Jinni on 2017/9/19 0019.
 */

public class GroupMemberChoiceAdapter extends BaseRecyclerViewAdapter<GroupMemberChoiceBean> {

    private Context                                 mContext;
    private OnItemClickListener                     mListener;
    private String mAvatar;

    public GroupMemberChoiceAdapter(Context context) {
        this.mContext = context;
    }

    public void setListener(OnItemClickListener listener) {
        mListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder;
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_group_member_choice, parent, false);
        holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            RequestOptions coverOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.mipmap.default_avatar)
                    .centerCrop()
                    .override(300, 300);
            final GroupMemberChoiceBean bean = mList.get(position);
            mAvatar = bean.getAvatar();
            Glide.with(mContext.getApplicationContext())
                    .load(TextUtils.isEmpty(mAvatar) ?
                            R.mipmap.default_avatar :
                            HttpRequestConstant.USER_PIC_HEAD + mAvatar)
                    .apply(coverOptions)
                    .into(((ViewHolder) holder).mIvMemberAvatar);
            ((ViewHolder) holder).mItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mListener.onItemClickListener(holder.getAdapterPosition(), bean);
                }
            });
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private View mItem;
        @BindView(R.id.member_avatar)
        ImageView mIvMemberAvatar;

        ViewHolder(View view) {
            super(view);
            mItem = view;
            ButterKnife.bind(this, view);
        }
    }

    public interface OnItemClickListener {
        void onItemClickListener(int position, GroupMemberChoiceBean member);
    }
}

